//#include "Local.h"
#include "LowPerf.h"
#include <math.h>

//////////////////////Functions/////////////////////////
/**
* @describe: real time butterworth filtering, fc = 5Hz, order 5;
* @param: input data point xn
* @return: filtered datapoing yn
* @author: Siyang Liang
* @date: 2017/03/29
*/
double butter_A::butter(double xn, int ChanID)
{
	double b[6] = { 2.139615203814e-05, 1.069807601907e-04, 2.139615203814e-04, 2.139615203814e-04, 1.069807601907e-04, 2.139615203814e-05 };//delete static before double;
    double a[5] = { -4.187300047864, 7.069722752792, -6.009958148187, 2.570429302524, -4.422091823996e-01 };  //delete static before double;
	double atemp = 0, btemp = 0, yn = 0;
	//static double bx[CHANID][6] = { 0 };
	//static double ax[CHANID][5] = { 0 };
	bx[ChanID][5] = bx[ChanID][4];
	bx[ChanID][4] = bx[ChanID][3];
	bx[ChanID][3] = bx[ChanID][2];
	bx[ChanID][2] = bx[ChanID][1];
	bx[ChanID][1] = bx[ChanID][0]; //right shift b by 1;
	bx[ChanID][0] = xn;

	yn = (bx[ChanID][0] * b[0] + bx[ChanID][1] * b[1] + bx[ChanID][2] * b[2] + bx[ChanID][3] * b[3] + bx[ChanID][4] * b[4] + bx[ChanID][5] * b[5]) - (ax[ChanID][0] * a[0] + ax[ChanID][1] * a[1] + ax[ChanID][2] * a[2] + ax[ChanID][3] * a[3] + ax[ChanID][4] * a[4]);

	ax[ChanID][4] = ax[ChanID][3];
	ax[ChanID][3] = ax[ChanID][2];
	ax[ChanID][2] = ax[ChanID][1];
	ax[ChanID][1] = ax[ChanID][0]; //right shift a by 1
	ax[ChanID][0] = yn;
	return yn;
}
